package dailyTopic;

import java.util.Arrays;

/**
 * @author zhangmin
 * @create 2021-08-03 9:29
 */
public class findUnsortedSubarray581 {

    public int findUnsortedSubarray(int[] nums) {
        int n = nums.length;
        int[] sortednums=new int[n];
        System.arraycopy(nums,0,sortednums,0,n);
        Arrays.sort(sortednums);
        int l=0;
        while (sortednums[l]==nums[l]){
            if (l==n-1){
                return 0;
            }else
                l++;
        }
        int r=n-1;
        while (sortednums[r]==nums[r]){
            if (r==0){
                return 0;
            }else
                r--;
        }
        return r-l+1;
    }



    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4};
        findUnsortedSubarray581 object = new findUnsortedSubarray581();
        System.out.println(object.findUnsortedSubarray(nums));
    }
}